package cn.com.cennavi.bean;

/**
 * 封闭道路红绿灯路口的地图属性数据
 * 
 * @ClassName: CloseRoadMapData
 * @Description: TODO（这里用一句话描述这个类的作用）
 * @author bull
 * @date 2016年7月8日 下午3:24:25
 *
 */
public class CloseRoadMapData {
	/**
	 * 道路首尾link方向
	 */
	public String roadId;
	/**
	 * link序列
	 */
	public String linkseq;
	/**
	 * 红绿灯路口id
	 */
	public String nodeId;
	/**
	 * 进路口的linkid
	 */
	public int inRoadId;
	/**
	 * 出路口的linkid
	 */
	public int outRoadId;
	/**
	 * link距离起始位置的距离序列
	 */
	public String disSeq;
	/**
	 * 分歧点前一共多长距离
	 */
	public int beforeRoadDis;
	/**
	 * 分歧点后一共多长距离
	 */
	public int afterRoadDis;
	/**
	 * 直行 与正北方向夹角 0-360
	 */
	public int angel;
	/**
	 * 样本数
	 */
	public int simpleNum;
	/**
	 * 标志位: 0:沿原道路继续行驶，1:出匝道，2:匝道汇入3:掉头4: 左转5: 右转
	 */
	public int flag;
	/**
	 * 
	 */
	public double lon;
	/**
	 * 
	 */
	public double lat;
	/**
	 * 转向标志 00表示左出，10表示右出，
	 */
	public String turnFlag;
	/**
	 * 总长度
	 */
	public int totalLen;

	public static CloseRoadMapData parseData(String line) {
		String[] lineArr = line.split("\\:");
		
		if (lineArr.length == 12) {
			CloseRoadMapData data = new CloseRoadMapData();
			data.linkseq = lineArr[0];
			data.roadId = getRoadId(data.linkseq);
			data.nodeId = lineArr[1];
			data.inRoadId = Integer.parseInt(lineArr[2]);
			data.outRoadId = Integer.parseInt(lineArr[3]);
			data.disSeq = lineArr[4];
			data.beforeRoadDis = Integer.parseInt(lineArr[5]);
			data.afterRoadDis = Integer.parseInt(lineArr[6]);
			data.angel = Integer.parseInt(lineArr[7]);
			data.simpleNum = Integer.parseInt(lineArr[8]);
			data.flag = Integer.parseInt(lineArr[9]);
			data.lon = Double.parseDouble(lineArr[10]);
			data.lat = Double.parseDouble(lineArr[11]);	
//			if(data.roadId.equals("12645748_12566710")){
//				System.out.println("断点处");
//			}
			data.totalLen = getTotalLength(data.linkseq,data.inRoadId,data.outRoadId,data.disSeq,data.beforeRoadDis,data.afterRoadDis);
			
			return data;
		}
		return null;
	}
	/**
	 * 因为地面道路在10字路口处，地图数据中的afterRoadDis字段没有包含十字路口内的link，所以要把10字路口内的link包含进来算道路总长度
	 * @param linkseq
	 * @param inRoadId
	 * @param outRoadId
	 * @param disSeq
	 * @param beforeRoadDis
	 * @param afterRoadDis
	 */
	private static int  getTotalLength(String linkseq, int inRoadId, int outRoadId, String disSeq, int beforeRoadDis, int afterRoadDis) {
		// TODO Auto-generated method stub
		String [] links=linkseq.split("\\|");
		String [] diss=disSeq.split("\\|");
		//得到出去link的位置
		int index=0;
		for(int i=0;i<links.length;i++){
			if(links[i].equals(outRoadId+"")){
				 index=i;
				 break;
			}
		}
		int totalLen=Integer.parseInt(diss[index])+afterRoadDis;
		return totalLen;
	}

	private static String getRoadId(String linkseq2) {
		// TODO Auto-generated method stub
		String[] arr = linkseq2.split("\\|");
		return arr[0] + "_" + arr[arr.length - 1];
	}

	public static CloseRoadMapData parseData1(String line) {
		String[] lineArr = line.split(",");
		if (lineArr.length == 15) {
			CloseRoadMapData data = new CloseRoadMapData();
			data.roadId = lineArr[0];
			data.linkseq = lineArr[1];
			data.nodeId = lineArr[2];
			data.inRoadId = Integer.parseInt(lineArr[3]);
			data.outRoadId = Integer.parseInt(lineArr[4]);
			data.disSeq = lineArr[5];
			data.beforeRoadDis = Integer.parseInt(lineArr[6]);
			data.afterRoadDis = Integer.parseInt(lineArr[7]);
			data.angel = Integer.parseInt(lineArr[8]);
			data.simpleNum = Integer.parseInt(lineArr[9]);
			data.flag = Integer.parseInt(lineArr[10]);
			data.lon = Double.parseDouble(lineArr[11]);
			data.lat = Double.parseDouble(lineArr[12]);
			data.turnFlag = lineArr[13];
			data.totalLen = Integer.parseInt(lineArr[14]);
			return data;
		}
		return null;
	}

	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append(roadId);
		sb.append(",");
		sb.append(linkseq);
		sb.append(",");
		sb.append(nodeId);
		sb.append(",");
		sb.append(inRoadId);
		sb.append(",");
		sb.append(outRoadId);
		sb.append(",");
		sb.append(disSeq);
		sb.append(",");
		sb.append(beforeRoadDis);
		sb.append(",");
		sb.append(afterRoadDis);
		sb.append(",");
		sb.append(angel);
		sb.append(",");
		sb.append(simpleNum);
		sb.append(",");
		sb.append(flag);
		sb.append(",");
		sb.append(lon);
		sb.append(",");
		sb.append(lat);
		return sb.toString();
	}
	public String toString2() {
		StringBuffer sb = new StringBuffer();
		sb.append(linkseq);
		sb.append(":");
		sb.append(nodeId);
		sb.append(":");
		sb.append(inRoadId);
		sb.append(":");
		sb.append(outRoadId);
		sb.append(":");
		sb.append(disSeq);
		sb.append(":");
		sb.append(beforeRoadDis);
		sb.append(":");
		sb.append(afterRoadDis);
		sb.append(":");
		sb.append(angel);
		sb.append(":");
		sb.append(simpleNum);
		sb.append(":");
		sb.append(flag);
		sb.append(":");
		sb.append(lon);
		sb.append(":");
		sb.append(lat);
		return sb.toString();
	}
	public String toString1() {
		StringBuffer sb = new StringBuffer();
		sb.append(roadId);
		sb.append(",");
		sb.append(linkseq);
		sb.append(",");
		sb.append(nodeId);
		sb.append(",");
		sb.append(inRoadId);
		sb.append(",");
		sb.append(outRoadId);
		sb.append(",");
		sb.append(disSeq);
		sb.append(",");
		sb.append(beforeRoadDis);
		sb.append(",");
		sb.append(afterRoadDis);
		sb.append(",");
		sb.append(angel);
		sb.append(",");
		sb.append(simpleNum);
		sb.append(",");
		sb.append(flag);
		sb.append(",");
		sb.append(lon);
		sb.append(",");
		sb.append(lat);
		sb.append(",");
		sb.append(turnFlag);
		sb.append(",");
		sb.append(totalLen);
		return sb.toString();
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String line = "679557|678702|678712:755486:679557:678702:0|213|315:213:207:207:74:0:116.43267:39.9733";
		CloseRoadMapData ss=CloseRoadMapData.parseData(line);
		System.out.println(ss.toString());
	}

}
